const { Builder } = require('selenium-webdriver')
let chrome = require('selenium-webdriver/chrome')

exports.initChromeDriver = function (option) {
  if (!option || !option.driverPath) {
    console.log("无chromedriver驱动路径")
    return null;
  }

  let driverPath = option.driverPath;
  let options = new chrome.Options()
  options.addArguments('--start-maximized') // 启动就最大化，而不是像后面再使用 maximize() 那样之后再最大化
  options.addArguments('--disable-popup-blocking')
  options.addArguments('no-sandbox')
  options.addArguments('disable-extensions')
  options.addArguments('no-default-browser-check')

  let service = new chrome.ServiceBuilder(driverPath).build()
  chrome.setDefaultService(service)
  if (option.headless) {
    return new Builder().forBrowser('chrome').setChromeOptions(options.headless()).build()
  }

  return new Builder().forBrowser('chrome').setChromeOptions(options).build()
}
